package cn.community.care.module.system.dal.mysql.orders;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import cn.community.care.framework.common.pojo.PageResult;
import cn.community.care.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.community.care.framework.mybatis.core.mapper.BaseMapperX;
import cn.community.care.module.system.dal.dataobject.hotproducts.HotProductsDO;
import cn.community.care.module.system.dal.dataobject.orders.OrdersDO;
import cn.community.care.module.system.dal.dataobject.orders.OrdersInfoDo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.ibatis.annotations.Mapper;
import cn.community.care.module.system.controller.admin.orders.vo.*;
import org.apache.ibatis.annotations.Select;

/**
 * 订单 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface OrdersMapper extends BaseMapperX<OrdersDO> {
    default List<Map<String, Object>> countTotalPriceByDayInLast7Days() {
        String startDate = LocalDateTime.now().minusDays(7).format(DateTimeFormatter.ISO_LOCAL_DATE);
        startDate = startDate + " 00:00:00";
        QueryWrapper<OrdersDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("DATE(create_time) AS date", "SUM(price) AS totalPrice")  // 选择日期和总金额
                .ge("create_time", startDate)  // 限制日期在7天之前
                .groupBy("DATE(create_time)")  // 按日期分组
                .orderByAsc("DATE(create_time)");  // 按日期升序排序
        return this.selectMaps(queryWrapper);  // 使用 selectMaps 获取结果
    }

    default Double calculateTotalSales() {
        // 创建查询条件
        QueryWrapper<OrdersDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("SUM(price) AS totalSales");  // 选择价格字段的总和

        // 执行查询并返回总额
        Map<String, Object> result = this.selectMaps(queryWrapper).stream()
                .findFirst()
                .orElse(new HashMap<>());  // 处理结果，防止为空

        // 获取总销售额
        Object totalSales = result.get("totalSales");
        return totalSales != null ? ((BigDecimal) totalSales).doubleValue() : 0.0;  // 返回计算的总销售额
    }

    default PageResult<OrdersDO> selectPage(OrdersPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrdersDO>()
                .eqIfPresent(OrdersDO::getOrderNumber, reqVO.getOrderNumber())
                .likeIfPresent(OrdersDO::getProductName, reqVO.getProductName())
                .eqIfPresent(OrdersDO::getProductSpecification, reqVO.getProductSpecification())
                .eqIfPresent(OrdersDO::getQuantity, reqVO.getQuantity())
                .eqIfPresent(OrdersDO::getPrice, reqVO.getPrice())
                .eqIfPresent(OrdersDO::getOrderStatus, reqVO.getOrderStatus())
                .eqIfPresent(OrdersDO::getCartId, reqVO.getCartId())
                .eqIfPresent(OrdersDO::getProductId, reqVO.getProductId())
                .eqIfPresent(OrdersDO::getCreator, reqVO.getCreator())
                .orderByDesc(OrdersDO::getId));
    }

    default PageResult<OrdersDO> selectPageByCart(OrdersPageReqVO reqVO) {
        return selectPage(reqVO, new LambdaQueryWrapperX<OrdersDO>()
                .inIfPresent(OrdersDO::getProductCartCode, reqVO.getProductCartCode())
                .orderByDesc(OrdersDO::getId));
    }

    //;
    @Select("select sum(price) as totalprice from  app_orders  where order_status  = 2")
    String queryPriceTotal();
    @Select("select count(*) as totalorders from  app_orders where deleted = false;")
    String queryOrderTotal();


    @Select("SELECT\n" +
            "    DATE(create_time) AS order_date,\n" +
            "    COUNT(id) AS daily_order_count\n" +
            "FROM\n" +
            "    app_orders\n" +
            "WHERE\n" +
            "    create_time >= CURDATE() - INTERVAL 7 DAY\n" +
            "    AND order_status = 2\n" +
            "GROUP BY\n" +
            "    order_date\n" +
            "ORDER BY\n" +
            "    order_date;")
    List<OrdersInfoDo> queryOrdersInfo();
}